package com.sun.module_home.ui.index

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.layout.AnimatedPane
import androidx.compose.material3.adaptive.navigation.NavigableListDetailPaneScaffold
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.sun.module_base.app.ResultState
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.utils.toJson
import com.sun.module_common.model.Article
import com.sun.module_home.model.BannerBean
import com.sun.module_common.model.ListWrapper
import com.sun.module_common.model.WebModel
import com.sun.module_common.route.RouteName
import com.sun.module_common.route.RouteUtils
import com.sun.module_common.webview.WebScreen
import com.sun.module_common.widget.RefreshScreen
import kotlinx.coroutines.launch

/**
 *@author:sunc
 *@time :2025/8/1 14:51
 *@desc: 体系
 */

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun IndexScreen(
    modifier: Modifier = Modifier,
    appThemeState: MutableState<AppThemeState> = mutableStateOf(AppThemeState()),
    viewModel: IndexViewModel = hiltViewModel<IndexViewModel>()
) {
    val list = remember { mutableStateListOf<Article>() }
    val topList = remember { mutableStateListOf<Article>() }
    val bannerList = remember { mutableStateListOf<BannerBean>() }

    LaunchedEffect(key1 = Unit) {
        viewModel.articleFlow.collect {
            when (it) {
                is ResultState.Loading -> {}
                is ResultState.Error -> {}

                is ResultState.Success<ListWrapper<Article>?> -> {
                    if (viewModel.isRefresh) {
                        list.clear()
                    }
                    it.data?.datas?.let { item ->
                        list.addAll(item)
                    }
                }
            }
        }
    }
    LaunchedEffect(Unit) {
        viewModel.bannerFlow.collect {
            when (it) {
                is ResultState.Loading -> {}
                is ResultState.Error -> {}

                is ResultState.Success<MutableList<BannerBean>?> -> {
                    bannerList.clear()
                    it.data?.let {
                        bannerList.addAll(it)
                    }
                }
            }
        }
    }

    LaunchedEffect(Unit) {
        viewModel.topArticleFlow.collect {
            when (it) {
                is ResultState.Loading -> {
                }

                is ResultState.Error -> {
                }

                is ResultState.Success<MutableList<Article>?> -> {
                    topList.clear()
                    it.data?.let {
                        topList.addAll(it)
                    }
                }
            }
        }
    }


    val navigator = rememberListDetailPaneScaffoldNavigator<Article>()
    val scope = rememberCoroutineScope()

    NavigableListDetailPaneScaffold(
        modifier = modifier then Modifier.fillMaxSize(),
        navigator = navigator,
        listPane = {
            AnimatedPane {
                RefreshScreen(
                    dataList = list,
                    appThemeState = appThemeState,
                    isRefreshing = viewModel.isRefresh,
                    isLoadMore = viewModel.isLoadMore,
                    onRefresh = {
                        viewModel.refresh()
                    },
                    onLoadMore = {
                        viewModel.loadMore()
                    }, itemClick = { item ->
                        RouteUtils.navigateTo(
                            RouteName.Web,
                            mapOf("webModel" to WebModel(item.title, item.link).toJson())
                        )
//                    scope.launch {
//                        navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
//                    }
                    }) {

                    ListTopItem(
                        topList = topList,
                        bannerList = bannerList,
                        appThemeState = appThemeState,
                        onSearch = {}) { item ->
                        RouteUtils.navigateTo(
                            RouteName.Web,
                            mapOf("webModel" to WebModel(item.title, item.link).toJson())
                        )
                    }
                }
            }
        },
        detailPane = {
            AnimatedPane {
                navigator.currentDestination?.contentKey?.let {
                    WebScreen(webData = WebModel(it.title, it.link)) {
                        scope.launch {
                            navigator.navigateBack()
                        }
                    }
//                    DetailScreen(data = it) {
//                        scope.launch {
//                            navigator.navigateBack()
//                        }
//                    }
                }

            }
        }
    )
}